//---------------------------------------------------------------------------
#include "MyBaseClass.h"
#ifdef MY_BORLAND
#include <vcl.h>
#pragma hdrstop
#pragma package(smart_init)
//---------------------------------------------------------------------------
static inline void ValidCtrCheck(MyBaseClass *)
{
	new MyBaseClass();
}
//---------------------------------------------------------------------------
#endif
#ifdef MY_BORLAND
MyBaseClass::MyBaseClass(void)
{
	Utility = new MyUtility();
	Utility->DebugLevel = FULL_DEBUG;
	Utility->OnDebugMsg = defaultDbgMsg;
	FDbgName = NULL;
	FLogFile = NULL;
	FLanguage = ENGLISH_LANG;
	FBaseClassVersion = BASECLASS_VER;
	FBaseClass_Configuration.AppendDataToLog = false;
}
#else
MyBaseClass::MyBaseClass(void):MyUtility()
{
//	Utility->DebugLevel = FULL_DEBUG;
	SetDebugLevel(FULL_DEBUG);
	//Utility->OnDebugMsg = defaultDbgMsg;
	FDbgName = NULL;
	FLogFile = NULL;
	FLanguage = ENGLISH_LANG;	
	FBaseClassVersion = BASECLASS_VER;	
	FBaseClass_Configuration.AppendDataToLog = false;	
}
#endif
//---------------------------------------------------------------------------
void MyBaseClass::ManageDebugMsg(DWORD DbgLevel, char *Format, ...)
{
	char *Tmp;
	char Mess[0x1000];
	bool isMess = true;
	memset(Mess,0,0x1000);
#ifdef MY_BORLAND	
	sprintf(Mess,"[%s]",Utility->GetNow());
#else	
    sprintf(Mess,"[%s]",GetNow());
#endif    
/*	Tmp = strstr(Mess,"[ERR]");
	if(Tmp != NULL)
		isMess = false;
	if(isMess)
	{
		Tmp = strstr(Mess,"[WRN]");
		if(Tmp != NULL)
			isMess = false;	
	}*/
	if(isMess)
	{
		switch(DbgLevel){
			case INFO_DEBUG:{
				sprintf(Mess+strlen(Mess),"[INF]");
			}break;
			case DETAIL_DEBUG:{
				sprintf(Mess+strlen(Mess),"[DTL]");
			}break;
			case VERBOSE_DEBUG:{
				sprintf(Mess+strlen(Mess),"[VBS]");
			}break;
			case FULL_DEBUG:{
				sprintf(Mess+strlen(Mess),"[FUL]");
			}break;
			case WARNING_DEBUG:{
				sprintf(Mess+strlen(Mess),"[WRN]");
			}break;
			case ERROR_DEBUG:{
				sprintf(Mess+strlen(Mess),"[ERR]");
			}break;
			
		};
	}
	if(FDbgName != NULL)
		sprintf(Mess+strlen(Mess),"[%s]",FDbgName);
	//sprintf(Mess,"%s",Temp);
	va_list args;
	va_start (args, Format);
	vsprintf (Mess+strlen(Mess),Format, args);
	va_end (args);
/*	Tmp = strstr(Mess,"[ERR]");
	int Len;
	if(Tmp != NULL)
	{
		Len = strlen(Tmp);
		memmove(Mess - 5, Mess, Len+1);
	}
	Tmp = strstr(Mess,"[WRN]");
	if(Tmp != NULL)
	{
		Len = strlen(Tmp);
		memmove(Mess - 5, Mess, Len+1);
	}	*/
#ifdef MY_BORLAND	
	Utility->OnDebugMsg(DbgLevel,Mess);
#else
	OnDebugMsg(DbgLevel,Mess);
#endif
}
//---------------------------------------------------------------------------
void MyBaseClass::ManageError(DWORD Code, DWORD SubCode, void *Add_Data,DWORD DbgLevel,char *Format,...)
{
#ifdef MY_BORLAND
	Utility->OnErrorEvt(Code,SubCode,Add_Data);
#else
	OnErrorEvt(Code,SubCode,Add_Data);
#endif
	char Mess[0x1000];
	memset(Mess,0,0x1000);
	//sprintf(Mess,"[ERR]");
  va_list args;
  va_start (args, Format);
  vsprintf (Mess+strlen(Mess),Format, args);
  va_end (args);
#ifdef MY_BORLAND  
  if(Utility->DebugLevel >=VERBOSE_DEBUG)
#else
  if(FDebugLevel >= VERBOSE_DEBUG)
#endif    
  {
	  sprintf(Mess+strlen(Mess),"\r\n    Cod. errore: 0x%08X - SubCod: 0x%08X",Code,SubCode);
	  if(Add_Data !=NULL)
		sprintf(Mess+strlen(Mess)," - Dati aggiuntivi presenti");
	}
	ManageDebugMsg(ERROR_DEBUG,Mess);
}
//---------------------------------------------------------------------------
void MyBaseClass::ManageWarning(DWORD Code, DWORD SubCode, void *Add_Data,DWORD DbgLevel,char *Format,...)
{
#ifdef MY_BORLAND
	Utility->OnWarningEvt(Code,SubCode,Add_Data);
#else
	OnWarningEvt(Code,SubCode,Add_Data);
#endif
	char Mess[0x1000];
	memset(Mess,0,0x1000);
	//sprintf(Mess,"[WRN]");
  va_list args;
  va_start (args, Format);
  vsprintf (Mess+strlen(Mess),Format, args);
  va_end (args);
#ifdef MY_BORLAND  
  if(Utility->DebugLevel >=VERBOSE_DEBUG)
#else
  if(FDebugLevel >= VERBOSE_DEBUG)
#endif   
  {
	  sprintf(Mess+strlen(Mess),"\r\n    Cod. warning: 0x%08X - SubCod: 0x%08X",Code,SubCode);
	  if(Add_Data !=NULL)
		sprintf(Mess+strlen(Mess)," - Dati aggiuntivi presenti");
	}
  ManageDebugMsg(WARNING_DEBUG,Mess);
}
//---------------------------------------------------------------------------
#ifdef MY_BORLAND
void MyBaseClass::defaultDbgMsg(DWORD Level, char *Mess)
{
	if(FLogFile != NULL)
	{
		if(Level<=Utility->DebugLevel)
			Utility->LogToFile(FLogFile,Mess);
	}
}
#else
void MyBaseClass::OnDebugMsg(DWORD Level, char *Mess)
{
	if(FLogFile != NULL)
	{
		if(Level<=FDebugLevel)
			LogToFile(FLogFile,Mess);
	}
}
#endif
//---------------------------------------------------------------------------
void MyBaseClass::setLogFile(char *F)
{
	if(F != NULL)
	{
#ifdef MY_BORLAND        
		FLogFile = Utility->DuplicateString(F);
#else
		FLogFile = DuplicateString(F);
#endif		
		if(!FBaseClass_Configuration.AppendDataToLog)
		{
		#ifdef UNICODE
			#ifdef MY_BORLAND
				DeleteFile(Utility->char_to_wchar(F));
			#else
				DeleteFile(char_to_wchar(F));
			#endif	
		#else
			DeleteFile(F);
		#endif
		}
	}
	else
		FLogFile = NULL;
}
//---------------------------------------------------------------------------
char *MyBaseClass::GetLogFile()
{    
#ifdef MY_BORLAND    
	return Utility->DuplicateString(FLogFile);
#else
    return DuplicateString(FLogFile);
#endif	
}
//---------------------------------------------------------------------------
char *MyBaseClass::TranslateErrorMsg(DWORD Code, DWORD SubCode, DWORD Lang)
{
    char Out[0x200];
    memset(Out,0,0x200);
    switch(Lang){
        case ITALIAN_LANG:{
            sprintf(Out,"Errore sconosciuto: cod. 0x%08X - sub. 0x08X",Code,SubCode);    
        }break;
        case ENGLISH_LANG:{
            sprintf(Out,"Unknown error: cod. 0x%08X - sub 0x%08X",Code,SubCode);        
        }break;
    };
    if(!Out[0])
        return NULL;
    else
#ifdef MY_BORLAND
        return Utility->DuplicateString(Out);
#else
        return DuplicateString(Out);
#endif                                    
}
//---------------------------------------------------------------------------
char *MyBaseClass::TranslateWarningMsg(DWORD Code, DWORD SubCode, DWORD Lang)
{
    char Out[0x200];
    memset(Out,0,0x200);
    switch(Lang){
        case ITALIAN_LANG:{
            sprintf(Out,"Warning sconosciuto: cod. 0x%08X - sub 0x%08X",Code,SubCode);    
        }break;
        case ENGLISH_LANG:{
            sprintf(Out,"Unknown warning: cod. 0x%08X - sub 0x%08X",Code,SubCode);        
        }break;
    };
    if(!Out[0])
        return NULL;
    else
#ifdef MY_BORLAND
        return Utility->DuplicateString(Out);
#else
        return DuplicateString(Out);
#endif                                    
}
//---------------------------------------------------------------------------
char *MyBaseClass::TranslateMess(DWORD Code, DWORD Lang)
{
    char Out[0x200];
    memset(Out,0,0x200);
    switch(Lang){
        case ITALIAN_LANG:{
            sprintf(Out,"Messaggio 0x%04X",Code);    
        }break;
        case ENGLISH_LANG:{
            sprintf(Out,"Message 0x%04X",Code);    
        }break;
    };
#ifdef MY_BORLAND
        return Utility->DuplicateString(Out);
#else
        return DuplicateString(Out);
#endif     
}
//---------------------------------------------------------------------------
void MyBaseClass::SetLanguage(DWORD NewLang)
{
    FLanguage = NewLang;
}
//---------------------------------------------------------------------------
DWORD MyBaseClass::GetLanguage()
{
    return FLanguage;
}
//---------------------------------------------------------------------------
DWORD MyBaseClass::GetBaseClassVersion()
{
    return FBaseClassVersion;   
}
//---------------------------------------------------------------------------
BaseClass_Conf MyBaseClass::GetBaseClassConf()
{
    BaseClass_Conf *Out = (BaseClass_Conf*)malloc(sizeof(BaseClass_Conf));
    memcpy(Out, &FBaseClass_Configuration,sizeof(BaseClass_Conf));
    return *Out;
}
//---------------------------------------------------------------------------
void MyBaseClass::SetBaseClassConf(BaseClass_Conf Value)
{
    memcpy(&FBaseClass_Configuration,&Value,sizeof(BaseClass_Conf));
}
//---------------------------------------------------------------------------
